var g_config = {
	groupGetted:false,
	authGetted:false,
	g_userId:null
};

$(function(){
	changeActiveItem("userList");
	init();
	initData();
	savehandle();
});

function init(){
	var id = location.hash;
	var ids = id.substring(1, id.length).split(',');
	g_config.g_userId = ids[0]; 
	g_config.group = ids[1];
	if(!heading.userId){
		heading.userId = g_config.g_userId;
		heading.group = g_config.g_group;
	}
	if(!g_config.g_userId){
		g_config.g_userId = heading.userId;
		g_config.group = heading.group;
	}
}
/**
 * 重写savehandle
 */
function savehandle(){
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap:{},
		successMsg:'配置成功',
		failmsg:'配置失败',
		success:successCallBack
	});
} 
/**
 * 用户权限配置成功跳转
 */
function successCallBack(){
	location.href="userList.jsp";
}

/**
 * 加载用户组配置信息
 */
function initData(){
	$.getJSON('/fw/auth/group_getAllGroups.action',function(data){
		if(data && data.rows){
			g_config.groupGetted=true;
			g_config.data1=data.rows;
			if(g_config.authGetted)
				setData();
		}
	});
	$.getJSON('/fw/auth/userGroup_getUserGroup.action',{'user.userId':g_config.g_userId},function(data){
		if(data && data.rows){
			g_config.authGetted=true;
			g_config.data2= data.rows;
			if(g_config.groupGetted)
				setData();
		}
	});
}

/**
 * 用户组配置信息列表显示
 */
function setData(){
	if(g_config.groupGetted){
		var data1 = g_config.data1,
			data2 = g_config.data2,
			s = '',s1='',
			len1 = data2.length;
		for(var i=0,len=data1.length;i<len;i++){
			var flag=0;
			for(var j=0;j<len1;j++){
				if(data2[j].groupName == data1[i].groupName){
					flag= 1;
					break;
				}
			}
			if(!flag){
				s += "<div class='checkbox'><label><input type='checkbox' value='"+ data1[i].groupId +"'>"+ data1[i].groupName +"</label></div>";
			}
			else{
				s1 += "<div class='checkbox'><label><input type='checkbox' value='"+ data1[i].groupId +"'>"+ data1[i].groupName +"</label></div>";
			}
		}
		$('#lPanel .panel-body').html(s);
		$('#rPanel .panel-body').html(s1);
	}else{
		setTimeout(setData,500);
	}
}

/**
 * 给用户配置一个组
 */
function toRight(){
	var ct = $($('#rPanel .panel-body')[0]);
	
	$("#lPanel :checkbox[checked]").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 移除用户一个组
 */
function toLeft(){
	var ct = $($('#lPanel .panel-body')[0]);
	$("#rPanel :checkbox[checked]").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 给用户配置所有组
 */
function allToRight(){
	var ct = $($('#rPanel .panel-body')[0]);
	$("#lPanel :checkbox").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 移除用户所有组配置
 */
function allToLeft(){
	var ct = $($('#lPanel .panel-body')[0]);
	$("#rPanel :checkbox").each(function(){
		$(this).removeAttr('checked');
		$(this).parent().parent().appendTo(ct);
	});
}
/**
 * 保存用户组配置信息
 */
function save(){
	var ida=[];
	$('#rPanel :checkbox').each(function(){
		ida.push($(this).val());
		
	});
	var ids = ida.join(',');
	if(ids.length == 0){
		$('#alertdiv').savehandle('setmsg',{msg:'用户组信息不能为空!',timer:2000});
		return;
	}
	$.getJSON(
		'/fw/auth/userGroup_setUserGroup.action',
		{'user.userId':g_config.g_userId,'group.ids':ids},
		function(data){
			$('#alertdiv').savehandle('handle',data,2000);
		}
	);
}
/**
 * 重置用户组配置
 */
function resetData(){
	initData();
}

